/*
 * $Id: CurriculumHourManager.java 8 2012-04-28 05:11:23Z forwardly@gmail.com $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.as.service.programme;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.as.dao.programme.CurriculumHourDao;
import com.lightsaber.trade.as.dao.programme.ProgrammeDao;
import com.lightsaber.trade.as.entity.programme.BaseProgramme;
import com.lightsaber.trade.as.entity.programme.CurriculumHour;
import com.lightsaber.trade.as.entity.programme.DurationType;
import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.service.BaseServiceImpl;

@Service
@Transactional
public class CurriculumHourManager extends BaseServiceImpl<CurriculumHour, String> {

    @Autowired
    CurriculumHourDao curriculumHourDao;
    @Autowired
    ProgrammeDao programmeDao;

    @Override
    protected BaseDao<CurriculumHour, String> getDao() {
        return curriculumHourDao;
    }

    public void onSaveProgramme(final BaseProgramme onlineProgramme) {

        Integer duration = 1;
        if (onlineProgramme.getDurationType() == DurationType.Year) {
            duration = onlineProgramme.getDuration();
        }

        CurriculumHour curriculumHour = null;
        for (int i = 1; i <= duration; i++) {

            curriculumHour = new CurriculumHour();
            curriculumHour.setProgramme(onlineProgramme);
            curriculumHour.setYear(i);

            curriculumHourDao.save(curriculumHour);
        }

    }

    public List<CurriculumHour> findByProgrammeId(final String programmeId) {
        return curriculumHourDao.findByProgrammeId(programmeId);
    }

    public void save(final List<CurriculumHour> curriculumHours) {
        if (curriculumHours == null || curriculumHours.size() <= 0) {
            return;
        }

        CurriculumHour old = null;
        for (CurriculumHour curriculumHour : curriculumHours) {
            old = curriculumHourDao.get(curriculumHour.getId());

            old.setHours(curriculumHour.getHours());
            curriculumHourDao.save(old);
        }

    }

    public CurriculumHour findByProgrammeIdAndYear(final String programmeId, final Integer year) {
        List<CurriculumHour> list = curriculumHourDao.findByProgrammeIdAndYear(programmeId, year);
        if (list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }
}
